package com.wd.health.model.adapter;

import android.content.Context;

import android.content.SharedPreferences;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;

import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
import com.shuyu.gsyvideoplayer.listener.GSYVideoProgressListener;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.wd.health.R;
import com.wd.health.app.App;
import com.wd.health.model.bean.VideoInfoBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @ProjectName:shopmall
 * @Package:com.wd.health.model.adapter
 * @ClassName:VideoAdapter
 * @UpdateUser: 裴童童
 * @date: 2020/12/22
 */
public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.ViewHolder>{
    private List<VideoInfoBean.ResultBean> result;
    private Context context;
    public static final String TAG = "ListNormalAdapter22";
    private GSYVideoOptionBuilder gsyVideoOptionBuilder;
    public VideoAdapter(List<VideoInfoBean.ResultBean> result, Context context) {
        this.result = result;
        this.context = context;
    }
    //创建开关弹幕变量
    private boolean isBarrage=false;

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(View.inflate(context,R.layout.item_video,null));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //视频标题赋值
        holder.list_video_adapter_title.setText(result.get(position).getTitle());
        //视频内容摘要
        holder.list_video_adapter_abstracts.setText(result.get(position).getAbstracts());
        //多少人购买
        holder.video_fragment_adapter_buyNum.setText(result.get(position).getBuyNum()+"人购买");
        //=======================================================================================================
        //判断是否收藏次视频
        //是否购买 1= 是2 =否
        final int whetherCollection = result.get(position).getWhetherCollection();
        if(whetherCollection==1){
            holder.video_fragment_adapter_collectvideos.setImageResource(R.mipmap.common_button_collection_small_s);
        }else{
            holder.video_fragment_adapter_collectvideos.setImageResource(R.mipmap.common_button_collection_small_n);
        }
        //给收藏设置监听事件
        holder.video_fragment_adapter_collectvideos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences user = holder.itemView.getContext().getSharedPreferences("user", Context.MODE_PRIVATE);
                boolean login = user.getBoolean("login", false);
                //判断是否登录
                if(login!=false){
                    //判断是否双收藏
                    if( result.get(position).getWhetherCollection()==1){
                        //如果是已经收藏的视频就取消收藏
                        cancelCollection.cancelCollection(result.get(position).getId());
                        holder.video_fragment_adapter_collectvideos.setImageResource(R.mipmap.common_button_collection_small_n);
                        //取消收藏就把实体类数据修改为2
                        result.get(position).setWhetherCollection(2);
                    }else {
                        holder.video_fragment_adapter_collectvideos.setImageResource(R.mipmap.common_button_collection_small_s);
                        //接口回调进行收藏
                        collection.collection(result.get(position).getId());
                        //已经收藏就把实体收藏数量为1
                        result.get(position).setWhetherCollection(1);
                    }
                }else{
                    Toast.makeText(App.getAppContext(), "请先登录", Toast.LENGTH_SHORT).show();
                }
            }
        });
        //=======================================================================================================

        //弹幕开关点击事件
        holder.video_fragment_adapter_danmu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isBarrage){
                    //关闭
                    holder.video_fragment_adapter_danmu.setImageResource(R.mipmap.common_icon_close_live_commenting_n);
                    isBarrage=false;
                    //调用弹幕关闭开关
                    stopBarrage.stopBarrage();
                }else{
                    //开启
                    holder.video_fragment_adapter_danmu.setImageResource(R.mipmap.common_icon_open_live_commenting_n);
                    isBarrage=true;
                    //调用开启弹幕开关
                    startBarrage.setStartBarrage(result.get(position).getId());
                }
            }
        });

        //=======================================================================================================

        //判断次视频是否购买
        final int whetherBuy = result.get(position).getWhetherBuy();
        //	是否购买 1= 是2 =否
        if(result.get(position).getWhetherBuy()==1){
            gsyVideoOptionBuilder.setUrl(result.get(position).getOriginalUrl());//设置视频地址  购买
            holder.video_fragment_adapter_goumai.setImageResource(R.mipmap.common_icon_comment_large_n);//购买图片赋值
        }else{
            holder.video_fragment_adapter_goumai.setImageResource(R.mipmap.common_icon_toll_n);//没有购买
            gsyVideoOptionBuilder.setUrl(result.get(position).getShearUrl());//设置视频地址  //试看
        }
        //判断是否是否购买
        holder.video_fragment_adapter_goumai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences user = holder.itemView.getContext().getSharedPreferences("user", Context.MODE_PRIVATE);
                boolean login = user.getBoolean("login", false);
                //判断是否登录
                if(login!=false){
                    //是否购买
                    if(whetherBuy==1){
                        //评论接口回调
                        comment.comment(result.get(position).getId());
                    }else{
                        holder.video_fragment_adapter_goumai.setImageResource(R.mipmap.common_icon_comment_large_n);//购买图片赋值
                        //购买接口回调
                        buy.buy(result.get(position).getId(),result.get(position).getPrice(),position);
                        notifyDataSetChanged();
                    }
                }else{
                    Toast.makeText(App.getAppContext(), "请先登录", Toast.LENGTH_SHORT).show();
                }
            }
        });

        //GSYVideoOptionBuilder

        Map<String, String> header = new HashMap<>();
        header.put("ee", "33");

        gsyVideoOptionBuilder.setGSYVideoProgressListener(new GSYVideoProgressListener() {
            @Override
            public void onProgress(int progress, int secProgress, int currentPosition, int duration) {

            }
        });


        //配置视频播放器参数
        gsyVideoOptionBuilder
                .setIsTouchWiget(false)
                //.setThumbImageView()设置第一帧
                .setCacheWithPlay(false)
                .setRotateViewAuto(true)
                .setLockLand(true)
                .setPlayTag(TAG)
                .setMapHeadData(header)
                .setShowFullAnimation(true)
                .setNeedLockFull(true)
                .setPlayPosition(position)
                .setReleaseWhenLossAudio(false)
                .setVideoAllCallBack(new GSYSampleCallBack() {
                    @Override
                    public void onPrepared(String url, Object... objects) {
                        super.onPrepared(url, objects);
                        if (!holder.standardGSYVideoPlayer.isIfCurrentIsFullscreen()) {
                            //静音
                            //GSYVideoManager.instance().setNeedMute(true);
                        }

                    }

                    @Override
                    public void onQuitFullscreen(String url, Object... objects) {
                        super.onQuitFullscreen(url, objects);
                        //全屏不静音
                        //GSYVideoManager.instance().setNeedMute(true);
                    }

                    @Override
                    public void onEnterFullscreen(String url, Object... objects) {
                        super.onEnterFullscreen(url, objects);
                        GSYVideoManager.instance().setNeedMute(false);
                        holder.standardGSYVideoPlayer.getCurrentPlayer().getTitleTextView().setText((String)objects[0]);
                    }


                }).build(holder.standardGSYVideoPlayer);

        //设置返回键
        holder.standardGSYVideoPlayer.getBackButton().setVisibility(View.GONE);

        //实现第一个视频自动播放
        if(position==0){
            holder.standardGSYVideoPlayer.startPlayLogic();
        }
    }


    @Override
    public int getItemCount() {
        return result==null?0:result.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private StandardGSYVideoPlayer standardGSYVideoPlayer;
        private TextView list_video_adapter_title;//标题
        private TextView list_video_adapter_abstracts;//视频摘要
        private TextView video_fragment_adapter_buyNum;//多少人购买
        private ImageView video_fragment_adapter_collectvideos;//是否收藏
        private ImageView video_fragment_adapter_danmu;//弹幕
        private ImageView video_fragment_adapter_goumai;//点击购买
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            gsyVideoOptionBuilder = new GSYVideoOptionBuilder();
            standardGSYVideoPlayer = itemView.findViewById(R.id.list_video_player);

            list_video_adapter_abstracts=itemView.findViewById(R.id.list_video_adapter_abstracts);//视频摘要
            list_video_adapter_title=itemView.findViewById(R.id.list_video_adapter_title);//标题
            video_fragment_adapter_buyNum=itemView.findViewById(R.id.video_fragment_adapter_buyNum);//多少人购买
            video_fragment_adapter_collectvideos=itemView.findViewById(R.id.video_fragment_adapter_collectvideos);//收藏图片
            video_fragment_adapter_danmu=itemView.findViewById(R.id.video_fragment_adapter_danmu);//弹幕
            video_fragment_adapter_goumai=itemView.findViewById(R.id.video_fragment_adapter_goumai);//点击购买
        }
    }

    //跳转收藏调用接口
    public interface Collection{
        void collection(int videoId);
    }
    private Collection collection;
    public void setCollection(Collection collection) {
        this.collection = collection;
    }

    //取消收藏Cancel,
    public interface CancelCollection{
        void cancelCollection(int videoId);
    }
    private CancelCollection cancelCollection;
    public void setCancelCollection(CancelCollection cancelCollection) {
        this.cancelCollection = cancelCollection;
    }
    //==========================================================================================
    //弹幕开启接口回调
    public interface StartBarrage{
        void setStartBarrage(int videoId);
    }
    public StartBarrage startBarrage;
    public void setStartBarrage(StartBarrage startBarrage) {
        this.startBarrage = startBarrage;
    }

    //创建弹幕关闭接口回调
    public interface StopBarrage{
        void stopBarrage();
    }
    private StopBarrage stopBarrage;
    public void setStopBarrage(StopBarrage stopBarrage) {
        this.stopBarrage = stopBarrage;
    }
    //=========================================================================================
    //购买回调
    public interface Buy{
        void buy(int videoId, int price, int position);
    }
    public Buy buy;
    public void setBuy(Buy buy) {
        this.buy = buy;
    }

    //评论回调
    public interface Comment{
        void comment(int videoId);
    }
    public Comment comment;
    public void setComment(Comment comment) {
        this.comment = comment;
    }

} 
